package testy;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseMotionEventDemo extends JPanel implements MouseMotionListener {
  private int mX, mY;
  private JComponent toDisp = null ;
  
  public MouseMotionEventDemo(JComponent comp) {
	  toDisp=comp;
	  comp.addMouseMotionListener(this);
	    comp.setVisible(true);
  }
  
  public MouseMotionEventDemo() {
    addMouseMotionListener(this);
    setVisible(true);
  }

  public void mouseMoved(MouseEvent me) {
    mX = (int) me.getPoint().getX();
    mY = (int) me.getPoint().getY();
    if (toDisp!=null) toDisp.repaint();
    else repaint();
  }

  public void mouseDragged(MouseEvent me) {
    mouseMoved(me);
  }

  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.fillRect(mX, mY, 3, 3);
  }

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new MouseMotionEventDemo());
    f.setSize(200, 200);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }

}
